//
//  MXCollocaCarefulChosenVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/12.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 搭配精选
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "MXCollocaCarefulChosenVC.h"
#import "MXCollocaHeaderView.h"
#import "AFNetworkHandler.h"
#import "MXCollocaSingleItemModel.h"
#import "MXCollocaSingleItemTableViewCell.h"
#import "MXCollocaSingleItemDetailVC.h"
#import "MBProgressHUD.h"
#import "MXDescribeTableViewCell.h"
@interface MXCollocaCarefulChosenVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
// 封装的商品图片的View
@property (nonatomic, retain) MXCollocaHeaderView *headerView;
// 数据的外层接受数组
@property (nonatomic, retain) NSMutableArray *careChosenArray;
// 单件商品的接收数组
@property (nonatomic, retain) NSMutableArray *singleItemArray;
// 商品描述的接收数组
@property (nonatomic, retain) NSMutableArray *desArray;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *alterView;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, retain) NSString *tempSize;
@end

@implementation MXCollocaCarefulChosenVC
- (void)dealloc
{
    [_tempSize release];
    [_timer release];
    [_HUD release];
    [_alterView release];
    [_desArray release];
    [_singleItemArray release];
    [_careChosenArray release];
    [_headerView release];
    [_careChosenID release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 全局数组初始化
    self.careChosenArray = [NSMutableArray array];
    self.singleItemArray = [NSMutableArray array];
    self.desArray = [NSMutableArray array];
    
    // 先铺视图
    [self creatTableView];
    
    // 引入数组
    [self GetData];
    
    // 添加返回和收藏Button
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 64)];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.text = @"";
        titleLabel.userInteractionEnabled = YES;
        [self.view addSubview:titleLabel];
        
        UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        backButton.frame = CGRectMake(WIDTHPRO * 10, HEIGHTPRO * 29, WIDTHPRO * 30, HEIGHTPRO * 30);
        [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
        [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
        [titleLabel addSubview:backButton];
        
        UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 50, HEIGHTPRO * 24, WIDTHPRO * 100, HEIGHTPRO * 40)];
        titleWord.text = @"搭配详情";
        titleWord.textAlignment = NSTextAlignmentCenter;
        titleWord.font = [UIFont systemFontOfSize:18];
        [titleWord setTextColor:[UIColor blueColor]];
        [titleLabel addSubview:titleWord];
    }

    
    // Do any additional setup after loading the view.
}

-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(uiActivityindicatorviewEnd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}

// 创建表视图
- (void)creatTableView
{
    // 高度减去headerView的高度
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHTPRO * 64, WIDTH, HEIGHT - HEIGHTPRO * 64) style:(UITableViewStylePlain)] autorelease];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
//    if (self.tempSize.length > 0) {
//        self.headerView = [[[MXCollocaHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 590)] autorelease];
//        
//    } else {
//        self.headerView = [[[MXCollocaHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 500)] autorelease];
//    }
    
    self.headerView = [[MXCollocaHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 560)];
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self.view addSubview:self.tableView];
    
    [self uiActivityindicatorviewStart];

}
/************************************* 解析数据部分 **************************************/
- (void)GetData
{
    NSString *careChosenPath = [NSString stringWithFormat:@"http://api.nanyibang.com/match-detail?collocation_id=%@", self.careChosenID];
    NSLog(@"%@", careChosenPath);
    
    [AFNetworkHandler GETwithAFNByURL:careChosenPath completion:^(id result) {
//===================================== 一级数据 ========================================
        NSDictionary *dataDiact = [result objectForKey:@"data"];
        
        MXCollocaCarefulChosenModel *model = [[[MXCollocaCarefulChosenModel alloc] initWithDic:dataDiact] autorelease];
        
        [self.desArray addObject:model.long_info];
        
//====================================== 二级数据 单件商品的数据 ============================

        for (NSDictionary *singleDict in model.singleItems) {
            
            MXCollocaSingleItemModel *singleModel = [[[MXCollocaSingleItemModel alloc] initWithDic:singleDict] autorelease];
            
            [self.singleItemArray addObject:singleModel];
        }
        // headerView 赋值方法
        [self.headerView GetCareChosenInfo:model];

        [self.careChosenArray addObject:model];
        
        [self.tableView reloadData];
        
        [self uiActivityindicatorviewEnd];
    }];
}
/************************************* TableView协议部分 **************************************/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return self.singleItemArray.count;
    } else {
        return self.careChosenArray.count;
    }
}

// 重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        MXCollocaSingleItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[MXCollocaSingleItemTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        }
        
        [cell getSingleItemInfo:self.singleItemArray[indexPath.row]];
        
        return cell;
    }
    
    MXDescribeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[MXDescribeTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"resue"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.content= self.desArray[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MXCollocaSingleItemDetailVC *detailVC = [[[MXCollocaSingleItemDetailVC alloc] init] autorelease];
        
        detailVC.singleItemDetailID = [self.singleItemArray[indexPath.row] item_id];

        // 使用模态跳转页面
        [self presentViewController:detailVC animated:YES completion:^{
            
            
        }];
    }
    
}

// 设置row的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHTPRO * 100;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/************************************* Button 点击效果 **************************************/

- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
